﻿using NLog;
using NLog.Targets;

namespace Ast.Framework.Logging
{
    /// <summary>
    /// The purpose of this NLog target is allowing extension to post logging message to host UI richbox
    /// </summary>
    [Target("LoggingMessageQueue")]
    public class LoggingMessageQueueTarget : Target
    {
        private readonly string _extensionName;
        private readonly ILoggingMessageQueue _loggingMessageQueue;

        public LoggingMessageQueueTarget(string extensionName, ILoggingMessageQueue loggingMessageQueue)
        {
            _extensionName = extensionName;
            _loggingMessageQueue = loggingMessageQueue;
        }

        protected override void Write(LogEventInfo logEvent)
        {
            // this is remoting call, have to make sure parameters are serializable
            _loggingMessageQueue.Enqueue(logEvent.Level.Ordinal, string.Format("[{0}]{1}", _extensionName, logEvent.LoggerName), logEvent.FormattedMessage);
        }
    }
}